﻿using System;
using System.Text;

namespace ESoft.Core.Utils.Extensions
{
    public static class ExceptionExtensions
    {
        /// <summary>
        ///     获取异常(日志)内容
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <returns>System.String.</returns>
        public static string GetIndentedExceptionLog(this Exception ex)
        {
            if (ex == null)
                return string.Empty;

            var sb = new StringBuilder(64);
            var innerExceptionLog = GetIndentedExceptionLog(ex.InnerException).Replace("\n", "\n\t");
            sb.AppendFormat(
                "Message: {1}{0}Type: {2}{0}StackTrace: {3}{0}InnerException: {4}",
                Environment.NewLine,
                ex.Message,
                ex.GetType().FullName,
                ex.StackTrace,
                innerExceptionLog);

            return sb.ToString();
        }
    }
}